using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;

namespace SCPutils
{
    /// <summary>
    /// Class for displaing the error messages inside the application
    /// </summary>
    public partial class ErrorMessage : Form
    {
        /// <summary>
        /// Stores the incoming Exception object for further processing
        /// </summary>
        private System.Exception ex;
        /// <summary>
        /// Stores the incoming user text
        /// </summary>
        private string UserText;
        /// <summary>
        /// Stores the flag for debug or production mode
        /// </summary>
        static public bool debugMode = false;


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="UserText"></param>
        /// <param name="et"></param>
        public ErrorMessage(Exception ex, string UserText, ErrorType et)
        {
            
            InitializeComponent();
            this.ex = ex;
            this.UserText = UserText;
            this.SetErrText(UserText);
            this.SetIcon(et);
            this.Height = 152;

            Log.WriteToLog(UserText, et);
            if (ex!=null)  Log.WriteToLog(ex.ToString(), et);

            if (!ErrorMessage.debugMode) this.cmd_Advanced.Visible = false;
            if (et == ErrorType.Warning || et == ErrorType.Info) this.cmd_Advanced.Visible = false;
            this.ShowDialog();
        }

        private void cmd_Advanced_Click(object sender, EventArgs e)
        {
            if (this.Height == 152)
            {
                this.Height = 328;
                this.cmd_Advanced.Text = SCPutils.Properties.Resource.txtSimple + " <<";
                cbo_HintSelect.SelectedIndex = 0;
            }
            else
            {
                this.Height = 152;
                this.cmd_Advanced.Text = Properties.Resource.txtExtended + " >>";
            }
        }

        private void cmd_OK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetIcon(ErrorType et)
        {

            switch (et)
            {
                case ErrorType.Critical:
                    this.Icon = new Icon(Utils.getEmbeddedResource("Icons.critical.ico"));
                    this.pbIcon.Image = Image.FromStream(Utils.getEmbeddedResource("Icons.critical.ico"));
                    break;
                case ErrorType.Warning:
                    this.Icon = new Icon(Utils.getEmbeddedResource("Icons.warning.ico"));
                    this.Text = "Warning";
                    this.pbIcon.Image = Image.FromStream(Utils.getEmbeddedResource("Icons.warning.ico"));
                    break;
                case ErrorType.Info:
                    this.Icon = new Icon(Utils.getEmbeddedResource("Icons.info.ico"));
                    this.Text = "Information";
                    this.pbIcon.Image = Image.FromStream(Utils.getEmbeddedResource("Icons.info.ico"));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Setting fault text
        /// </summary>
        /// <param name="Text"></param>
        private void SetErrText(string Text)
        {
            if (Text != null)
            {
                this.lblErrText.Text = UserText;
            }
            else
            {
                this.lblErrText.Text = ex.Message;
            }
        }

        private void cbo_HintSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                switch (this.cbo_HintSelect.SelectedItem.ToString())
                {
                    case "Inner Exception":
                        this.rtbErrorMsg.Text = ex.InnerException.Message;
                        break;
                    case "Error Message":
                        this.rtbErrorMsg.Text = ex.Message;
                        break;
                    case "Stack Trace":
                        this.rtbErrorMsg.Text = ex.StackTrace;
                        break;
                    case "Source":
                        this.rtbErrorMsg.Text = ex.Source;
                        break;
                    case "Exception Type":
                        this.rtbErrorMsg.Text = ex.GetType().ToString();
                        break;
                    default:
                        break;
                }
            }
            catch { }
        }
    }

    /// <summary>
    /// Class containing static methods for rapid call of ErrorMessage form
    /// </summary>
    public static class ErrorMsg
    {
        /// <summary>
        /// Shows the ErrorMessage window for Critical errors
        /// </summary>
        /// <param name="ex"></param>
        public static void showCritical(Exception ex)
        {
            ErrorMessage errMsg = new ErrorMessage(ex, ex.Message, ErrorType.Critical);
        }

        /// <summary>
        /// Shows the ErrorMessage window for Critical errors
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="UserText"></param>
        public static void showCritical(Exception ex, string UserText)
        {
            ErrorMessage errMsg = new ErrorMessage(ex, UserText, ErrorType.Critical);
        }
        /// <summary>
        /// Shows the ErrorMessage window for Warnings messages
        /// </summary>
        /// <param name="UserText"></param>
        public static void showWarning(string UserText)
        {
            ErrorMessage errMsg = new ErrorMessage(null, UserText, ErrorType.Warning);
        }
        /// <summary>
        /// Shows the ErrorMessage window for Information messages
        /// </summary>
        /// <param name="UserText"></param>
        public static void showInfo(string UserText)
        {
            ErrorMessage errMsg = new ErrorMessage(null, UserText, ErrorType.Info);
        }

    }
}